﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProjectStorageDB;
using ProjectStorageLib;

public partial class Commercial_Registration : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lMess.Visible = false;
        Reg.ValidationExpression = @"^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$";

    }

    protected void bSave_Click(object sender, EventArgs e)
    {
        if (!Page.IsValid)
            return;

        if (tbPassWord.Text != tbPassWord2.Text)
        {
            lMess.Visible = true;
            lMess.InnerText = "Different passwords!";

            tbPassWord.Text = "";
            tbPassWord2.Text = "";

            return;
        }

        //locals       
        ProjectStorageLib.User.UserProcessor proc = null;
        
        try
        {
            //Create processor 
            proc = new ProjectStorageLib.User.UserProcessor();

            if (proc.IsUserExist(tbName.Text.Trim()))
            {
                lMess.Visible = true;
                lMess.InnerText = "There is other user with same name!";

                return;
            }

            //Create object
            User us = new User();

            //Init fields
            us.name = tbName.Text;
            us.user_name = tbUserName.Text;
            us.password = tbPassWord.Text;

            if (tbMail.Text.Trim() != "")
                us.mail = tbMail.Text.Trim();

            //Save object
            proc.AddUser(us);

            //notifay use
            lMess.Visible = true;
            lMess.InnerText = "Success!";
        }
        catch (Exception err)
        {
            lMess.InnerText = err.Message;
        }
        finally
        {
            if (proc != null)
            {
                proc.Dispose();
                proc = null;
            }
        }
    }
}